<template>
  <div>
    <el-row>
      <el-input
        v-model="searchObj.username"
        placeholder="请输入内容"
        style="width: 200px; margin-right: 10px"
      ></el-input>
      <el-button
        type="primary"
        icon="el-icon-search"
        @click="search"
        :disabled="searchObj.username !== '' ? false : true"
        >查询</el-button
      >
      <el-button @click="clear">清空</el-button>
    </el-row>
    <el-row>
      <el-button type="primary" @click="showAddUser">添加</el-button>
      <el-button type="warning" @click="removeUsers">批量删除</el-button>
    </el-row>
    <el-row>
      <el-table
        ref="multipleTable"
        :data="users"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        border
        v-loading="listLoading"
        @select="selectUser"
      >
        <el-table-column type="selection" width="55" align="center">
        </el-table-column>
        <el-table-column label="序号" width="80" type="index" align="center">
        </el-table-column>
        <el-table-column prop="username" label="用户名" width="width">
        </el-table-column>
        <el-table-column prop="nickName" label="用户昵称" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="roleName" label="权限列表" show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          prop="gmtCreate"
          label="创建时间"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="gmtModified"
          label="更新时间"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column prop="prop" label="操作" show-overflow-tooltip>
          <template slot-scope="{ row, $index }">
            <el-button
              type="info"
              icon="el-icon-warning-outline"
              size="mini"
              @click="infoUser(row)"
            ></el-button>
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="upDataUser(row)"
            ></el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="getUsers"
        :current-page.sync="page"
        :page-sizes="[3, 5, 10]"
        :page-size="limit"
        layout=" prev, pager, next, jumper,->,total, sizes"
        :total="total"
        background
      >
        :pager-count="7">
      </el-pagination>
    </el-row>
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form
        :model="formUser"
        label-width="100px"
        :rules="rules"
        ref="ruleForm"
      >
        <el-form-item label="用戶名" prop="username">
          <el-input v-model="formUser.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用戶昵称" prop="nickname">
          <el-input v-model="formUser.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用戶秘密" prop="password">
          <el-input v-model="formUser.password" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="addUser('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="formInfoVisible" title="设置角色">
      <el-form
        :model="formInfo"
        label-width="100px"
        :rules="rulesInfo"
        ref="ruleFormInfo"
      >
        <el-form-item label="用戶名" prop="username">
          <el-input v-model="formInfo.username" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import userAPI from "@/api/acl/user";
export default {
  data() {
    return {
      users: [],
      limit: 10,
      page: 1,
      total: 0,
      // 是否显示加载提示
      listLoading: false,
      // 请求搜索条件数据的对象
      searchObj: {
        username: "",
      },
      // 已勾选（进行批量删除）的用户数组
      selectedIds: [],
      // 搜集搜索条件输入的对象
      tempSearchObj: {
        username: "",
      },
      dialogFormVisible: false,
      formInfoVisible: false,
      // 弹出框的表单数据
      formUser: {
        username: "",
        nickname: "",
        password: "",
      },
      formInfo: {
        username: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 1,
            max: 15,
            message: "长度在 1 到 15 个字符",
            trigger: "blur",
          },
        ],
        nickname: [
          { required: true, message: "请输入用昵称", trigger: "blur" },
          {
            min: 1,
            max: 15,
            message: "长度在 1 到 15 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 35,
            message: "长度在 6 到 35 个字符",
            trigger: "blur",
          },
        ],
      },
      rulesInfo: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 1,
            max: 15,
            message: "长度在 1 到 15 个字符",
            trigger: "blur",
          },
        ],
      },

      // 当前编辑的哪一项的数据
      editRow: {},
    };
  },
  methods: {
    handleSizeChange(val) {
      this.limit = val;
      this.getUsers();
    },
    handleSelectionChange() {},
    search() {
      this.tempSearchObj = { ...this.searchObj };
      this.getUsers();
    },
    clear() {
      this.searchObj.username = "";
      this.tempSearchObj.username = "";
      this.getUsers();
    },
    showAddUser() {
      this.dialogFormVisible = true;
    },
    addUser(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dialogFormVisible = false;
          this.formUser = {
            username: "",
            nickname: "",
            password: "",
          };
        } else {
          this.$message.error("请完善表单信息！");
          return false;
        }
      });
    },
    upDataUser(row) {
      this.dialogFormVisible = true;
      this.formUser = {
        username: row.username,
        password: row.password,
        nickname: row.nickname,
      };
      console.log(row);
    },
    cancle(formName) {
      this.$refs[formName].resetFields();
      this.dialogFormVisible = false;
      this.formUser = {
        username: "",
        nickname: "",
        password: "",
      };
    },
    // 批量删除
    removeUsers() {},
    // 勾选
    selectUser(selection, row) {
      console.log(selection, "selection");
      console.log(row, "row");
      let selected = selection.length && selection.indexOf(row) !== -1;
      console.log(selected);
    },
    // 获取分页列表
    async getUsers(page = 1) {
      this.page = page;
      const { limit, searchObj } = this;
      this.listLoading = true;
      let result = await userAPI.getPageList(this.page, limit, searchObj);
      this.listLoading = false;
      if (result.code === 20000) {
        // console.log(result.data)
        const { items, total } = result.data;
        // 要把里面的admin过滤出来
        this.users = items.filter((item) => item.username !== "admin");
        this.total = total - 1;
      }
    },
    // info信息
    infoUser(row) {
      console.log(row);
      this.formInfoVisible=true
    },
  },
  mounted() {
    this.getUsers();
    console.log(userAPI);
  },
};
</script>

<style lang="scss" scoped>
.el-row {
  margin: 15px 0;
}
</style>
